package com.guisu.material.recyclerview

import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.guisu.material.R

class RecyclerActivity : AppCompatActivity() {
    private val list = mutableListOf<String>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler)
        for (i in 0..50) {
            list.add("$i")
        }
        val rlv = findViewById<LoadRefreshRecyclerView>(R.id.rlv)
        rlv.layoutManager = LinearLayoutManager(this)
        val adapter = CommonItemAdapter()
        adapter.data = list
        rlv.adapter = adapter
        val headerView =
            LayoutInflater.from(this).inflate(R.layout.layout_recycler_header, rlv, false)
        val footerView =
            LayoutInflater.from(this).inflate(R.layout.layout_recycler_footer, rlv, false)
        //设置上拉刷新
        rlv.addRefreshViewCreator(CommonRefreshCreator())
        //添加Header
        rlv.addHeaderView(headerView)
        //添加Footer
        rlv.addFooterView(footerView)
        //添加下拉加载更多 【由于内部是通过addView实现，存在添加顺序，需要注意】
        rlv.addLoadViewCreator(CommonLoadCreator())
        rlv.setOnRefreshListener(object : RefreshRecyclerView.OnRefreshListener {
            override fun onRefresh() {
                rlv.postDelayed({
                    rlv.onStopRefresh()
                }, 2000)
            }

        })
        rlv.setOnLoadMoreListener(object : LoadRefreshRecyclerView.OnLoadMoreListener {
            override fun onLoad() {
                rlv.postDelayed({
                    rlv.onStopLoad()
                }, 2000)
            }

        })

    }

}
